package projectxxx;

import java.sql.*;
import javax.swing.JTable;
import javax.swing.JOptionPane;

public class StockManager 
{
	private String myDriver = "com.mysql.jdbc.Driver";
	private String ipOfDB = "localhost";
	private MySQLdriverConnection msc;
	private Connection Conn;
	Stock stock;
	MainStockGui ms;
	
	public StockManager() 
	{
		
		try
		{
			msc = new MySQLdriverConnection("","",ipOfDB,"turtleData");
			Conn = msc.getConnection();
		}
		catch(SQLException sqlE)
		{
			sqlE.printStackTrace();
		}	
		catch(ClassNotFoundException cnfe)
		{
			cnfe.printStackTrace();
		}
	}

	public void commitStock(Stock stock)
	{
		try
		{
			this.stock = stock;
			String [] str = stock.getStockString();
			Statement stmt = Conn.createStatement();
			String update;
			for(int i = 0; i < str.length; i++)
			{
				System.out.println(str[i]);
			}
			
			//Must fix this code with proper get methods
			update = "INSERT INTO stockTable VALUES('"+
				str[0]+"','"+str[1]+"','"+str[2]+"','"+str[3]+"','"+
				str[4]+"','"+str[5]+"','"+str[6]+"','"+str[7]+"','"+
				str[8]+"','"+str[9]+"','"+str[10]+"','"+str[11]+"','"+
				str[12]+"','"+str[13]+"','"+str[14]+"','"+
				str[15]+"', '"+str[16]+"')";
			int rows = stmt.executeUpdate(update);
			System.out.println("Rows changed: "+rows);
		}
		catch(SQLException sqlx)
		{
			sqlx.printStackTrace();
		}
	}
	
	public Stock findStock(String inString, String typeString)
	{
		String input = inString;
		String type = typeString;
		try
		{
			String queryStockID = "SELECT stockId, stockTypeId, title,"+ 
							"status, barcode, brand, serialNo, description,"+ 
							"catId, runTime, releaseDate, genre, age,"+ 
							"director, actor1, actor2, dateLastRented"+ 
							" FROM stockTable "+ 
							"WHERE stockId LIKE ?";
			String queryTitle = 	"SELECT stockId, stockTypeId, title,"+ 
							"status, barcode, brand, serialNo, description,"+ 
							"catId, runTime, releaseDate, genre, age,"+ 
							"director, actor1, actor2, dateLastRented"+ 
							" FROM stockTable "+ 
							"WHERE title LIKE ?";
			String queryBarcode = "SELECT stockId, stockTypeId, title,"+ 
							"status, barcode, brand, serialNo, description,"+ 
							"catId, runTime, releaseDate, genre, age,"+ 
							"director, actor1, actor2, dateLastRented"+ 
							" FROM stockTable "+ 
							"WHERE barcode LIKE ?";				
			
			PreparedStatement pstmt;
			ResultSet rs;
			ms = new MainStockGui();
			String temp = (String)ms.searchJcb.getSelectedItem();
			if(temp.equals("Stock ID"))
			{
				pstmt = Conn.prepareStatement(
						queryStockID);
				System.out.println("Stock ID");
				pstmt.setString(1, input);
				rs = pstmt.executeQuery();
			}
			else if(temp.equals("Barcode"))
			{
				pstmt = Conn.prepareStatement(
						queryBarcode);
				System.out.println("Barcode");
				pstmt.setString(1, input);
				rs = pstmt.executeQuery();
			}
			else if(temp.equals("Title"))
			{
				pstmt = Conn.prepareStatement(
						queryTitle);
				System.out.println("Title");
				pstmt.setString(1, input);
				rs = pstmt.executeQuery();
			}
			else
			{
				pstmt = Conn.prepareStatement(
						queryStockID);
				System.out.println("Stock ID");
				pstmt.setString(1, input);
				rs = pstmt.executeQuery();
			}
			
			if(rs.next())
			{
				rs.first();
				String [] queryArray = new String [17];
				queryArray[0] = rs.getString("stockId");
				queryArray[1] = rs.getString("stockTypeId");
				queryArray[2] = rs.getString("title");
				queryArray[3] = rs.getString("status");
				queryArray[4] = rs.getString("barcode");
				queryArray[5] = rs.getString("brand");
				queryArray[6] = rs.getString("serialNo");
				queryArray[7] = rs.getString("description");
				queryArray[8] = rs.getString("catId");
				queryArray[9] = rs.getString("runTime");
				queryArray[10] = rs.getString("releaseDate");
				queryArray[11] = rs.getString("genre");
				queryArray[12] = rs.getString("age");
				queryArray[13] = rs.getString("director");
				queryArray[14] = rs.getString("actor1");
				queryArray[15] = rs.getString("actor2");
				queryArray[16] = rs.getString("dateLastRented");
				
				Stock s = new Stock(queryArray);
		
				return s;
			}
			else
			{
				return null;
			}
		}
		catch(SQLException sqlx)
		{
			sqlx.printStackTrace();
			return null;
		}
	}
		
	public String generateStockId()
	{
		String uniqueId = null;
		try
		{
			Statement stockIdGeneratorSTMT = Conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
			ResultSet stockIdGeneratorRS;
			String stockIdGeneratorQUERY = "SELECT stockId FROM stockTable";
		
			stockIdGeneratorRS = stockIdGeneratorSTMT.executeQuery(stockIdGeneratorQUERY);
		
			stockIdGeneratorRS.last();
			String lastId = stockIdGeneratorRS.getString(1);
			String tempId = lastId.substring(2);
			int incrementer = Integer.parseInt(tempId);
			incrementer++;
			uniqueId = new String("si"+incrementer);
		
			
		}
		catch(SQLException sqlE)
		{
			sqlE.printStackTrace();
		}	
		return uniqueId;
	
	}//end generateStockId()
}
